/* 
路由过滤器
2018-11-16
*/
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from './index'
import store from '@/store'

export default function filter() {
    NProgress.configure({ showSpinner: false })
    router.beforeEach(async (to, from, next) => {
        NProgress.start()
        //设置标题
        window.document.title = to.meta.title
        //判断动态路由是否加载

        if (to.name == 'login') {
            next()
            return
        }
        //验证客户端账号信息是否存在，
        if (store.getters.user) {
            //设置header栏面包屑
            if (to.matched.some(record => record.meta.auth)) {
                store.commit('SET_BREADCRUMB', to.meta.breadcrumb)
            }
            next()
        } else {
            //服务器验证账号是否还在登录状态
            store.dispatch('user_check')
                .then(res => {
                    if (res.code === 200) {
                        next(to.path)
                    } else {
                        next({ name: 'login' })
                    }
                }).catch(err => {
                    next({ name: 'login' })
                })
        }

    })

    router.afterEach(() => {
        NProgress.done()
    })
}

